package com.ymatou.iapi.livebuyer.testcase;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.google.gson.JsonArray;
import com.ymatou.iapi.livebuyer.parameter.QuerySecKillProductsBean;
import com.ymatou.iapi.livebuyer.service.QuerySecKillProductsCall;
import com.ymatou.iapi.liveseller.parameter.CreateLiveActivityBean;
import com.ymatou.iapi.liveseller.parameter.CreateSellerLiveBean;
import com.ymatou.iapi.liveseller.parameter.ModifyLiveActivityBean;
import com.ymatou.iapi.productmanager.parameter.product.AddSellerProductRequest;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.LiveRoomCallService;
import com.ymttest.business.service.ProductManagerCallServiceV2;
import com.ymttest.database.model.LiveRoom;
import com.ymttest.database.model.YmtCatalogsI;
import com.ymttest.database.sqlwapper.YmtCatalogsIWapper;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.YMTDateUtil;
/**
*限时秒杀商品列表
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_QuerySecKillProducts{
	private static QuerySecKillProductsBean queryseckillproductsBean;
	private static QuerySecKillProductsCall queryseckillproductsCall;
	private static int sellerId = Integer.parseInt(EnvSetup.getData("autotest_seller1"));
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("限时秒杀商品列表");
	}

	@Before
	public void caseUp() {
		queryseckillproductsBean=new QuerySecKillProductsBean();
		queryseckillproductsCall=new QuerySecKillProductsCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(queryseckillproductsCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_QuerySecKillProducts_001() {
		Logger.start(true,"限时秒杀商品列表,成功");
		try {
			Logger.comment("创建商品");
			AddSellerProductRequest addpro = ProductManagerCallServiceV2.initAddSellerProductRequest(sellerId);
			String productId = ProductManagerCallServiceV2.addNewProduct(addpro).get("Data").getAsJsonObject().get("ProductId").getAsString();
			Logger.comment("创建直播");
			// 5分钟后开始200分钟后结束
			CreateSellerLiveBean clive = LiveRoomCallService.initCreatesellerliveBean(sellerId);
			Date now = YMTDateUtil.getDate();
			//半小时之内会锁定不能修改
			Date starttime = new Date(now.getTime() + 10 * 1000);
			Date endtime = new Date(now.getTime() + 200 * 60 * 1000);
			clive.getLiveDto().setStartTime(YMTDateUtil.getFormatTime(starttime, YMTDateUtil.YMDHMS));
			
			LiveRoom createlive = LiveRoomCallService.CreateSellerLive(clive);
			Logger.comment("加入商品");
			LiveRoomCallService.AddProductsToLive(createlive.getId(), sellerId, productId);
			Logger.comment("查询商品规格");
			YmtCatalogsIWapper CIWapper = new YmtCatalogsIWapper();
			List<String> cids = CIWapper.selectCatalogsByProductId(productId).stream().map(YmtCatalogsI::getsCatalogId).collect(Collectors.toList());
			Logger.comment("创建秒杀活动");
			CreateLiveActivityBean createliveactivityBean = LiveRoomCallService.initCreateLiveActivityBean(createlive.getId(), cids.get(0), 5d, 6, starttime);
			LiveRoomCallService.CreateLiveActivity(createliveactivityBean);
			ModifyLiveActivityBean ModifyLiveActivityBean = LiveRoomCallService.initModifyLiveActivityBean(createliveactivityBean);
			LiveRoomCallService.SellerLiveOnline(createlive.getId(), sellerId);
			queryseckillproductsBean.setMaxNum(5);
			queryseckillproductsCall.setData(queryseckillproductsBean);
			queryseckillproductsCall.callService();			
			Logger.verifyEquals("200", queryseckillproductsCall.getString("code"), "判断code");
			JsonArray ldata = queryseckillproductsCall.getJsonObject().get("data").getAsJsonArray();
			Logger.verifyEquals(true, ldata.toString().contains(productId), "判断会返回30分钟内的productid");
//			1）进行中的直播
//			2）商品有效且在售状态
//			3）秒杀活动未开始
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_QuerySecKillProducts_002() {
		Logger.start(true,"不传参数maxNum失败");
		try {
			queryseckillproductsCall.setData(queryseckillproductsBean);
			queryseckillproductsCall.callService();			
			Logger.verifyEquals("100", queryseckillproductsCall.getString("code"), "判断code");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_QuerySecKillProducts_003() {
		Logger.start(true,"maxNum小于0,失败");
		try {
			queryseckillproductsBean.setMaxNum(-1);
			queryseckillproductsCall.setData(queryseckillproductsBean);
			queryseckillproductsCall.callService();			
			Logger.verifyEquals("100", queryseckillproductsCall.getString("code"), "判断code");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_QuerySecKillProducts_004() {
		Logger.start(true,"maxNum大于20,失败");
		try {
			queryseckillproductsBean.setMaxNum(21);
			queryseckillproductsCall.setData(queryseckillproductsBean);
			queryseckillproductsCall.callService();			
			Logger.verifyEquals("100", queryseckillproductsCall.getString("code"), "判断code");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	@Ignore
	public void Tc_QuerySecKillProducts_005() {
		//业务测试赛需求调整到600分钟
		Logger.start(true,"限时秒杀商品列表,30分钟之后的不显示");
		try {
			Logger.comment("创建商品");
			AddSellerProductRequest addpro = ProductManagerCallServiceV2.initAddSellerProductRequest(sellerId);
			String productId = ProductManagerCallServiceV2.addNewProduct(addpro).get("Data").getAsJsonObject().get("ProductId").getAsString();
			Logger.comment("创建直播");
			// 5分钟后开始200分钟后结束
			CreateSellerLiveBean clive = LiveRoomCallService.initCreatesellerliveBean(sellerId);
			Date now = YMTDateUtil.getDate();
			//半小时之内会锁定不能修改
			Date starttime = new Date(now.getTime() + 35*60 * 1000);
			Date endtime = new Date(now.getTime() + 200 * 60 * 1000);
			clive.getLiveDto().setStartTime(YMTDateUtil.getFormatTime(starttime, YMTDateUtil.YMDHMS));
			
			LiveRoom createlive = LiveRoomCallService.CreateSellerLive(clive);
			Logger.comment("加入商品");
			LiveRoomCallService.AddProductsToLive(createlive.getId(), sellerId, productId);
			Logger.comment("查询商品规格");
			YmtCatalogsIWapper CIWapper = new YmtCatalogsIWapper();
			List<String> cids = CIWapper.selectCatalogsByProductId(productId).stream().map(YmtCatalogsI::getsCatalogId).collect(Collectors.toList());
			Logger.comment("创建秒杀活动");
			CreateLiveActivityBean createliveactivityBean = LiveRoomCallService.initCreateLiveActivityBean(createlive.getId(), cids.get(0), 5d, 6, starttime);
			LiveRoomCallService.CreateLiveActivity(createliveactivityBean);
			ModifyLiveActivityBean ModifyLiveActivityBean = LiveRoomCallService.initModifyLiveActivityBean(createliveactivityBean);
			LiveRoomCallService.SellerLiveOnline(createlive.getId(), sellerId);
			queryseckillproductsBean.setMaxNum(5);
			queryseckillproductsCall.setData(queryseckillproductsBean);
			queryseckillproductsCall.callService();			
			Logger.verifyEquals("200", queryseckillproductsCall.getString("code"), "判断code");
			JsonArray ldata = queryseckillproductsCall.getJsonObject().get("data").getAsJsonArray();
			Logger.verifyEquals(false, ldata.toString().contains(productId), "判断不会返回30分钟后的productid");
//			1）进行中的直播
//			2）商品有效且在售状态
//			3）秒杀活动未开始
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}